package com.sl.au.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.DateQueryItem;
import com.sl.au.entity.DoubleQueryItem;
import com.sl.au.entity.IntegerQueryItem;
import com.sl.au.entity.M2R;
import com.sl.au.entity.Module;
import com.sl.au.entity.NumQueryItem;
import com.sl.au.entity.User;
import com.sl.au.service.AccountService;
import com.sl.au.service.M2RService;
import com.sl.au.service.ModuleService;
import com.sl.utils.DataConverter;

public class BaseController implements ErrorController {

	private static final String PATH = "/error";
	protected static Module module = null;

	@Autowired
	HttpServletRequest request;

	@Autowired
	M2RService m2rService;
	@Autowired
	ModuleService moduleService;
	@Autowired
	AccountService accountService;

	public ModelAndView createLayoutView(String rightViewName) {
		ModelAndView mv = new ModelAndView("layout/layout");
		mv.addObject("rightViewName", rightViewName);
		mv.addObject("canReadCustomer", this.canReadCustomer());
		return mv;
	}

	public ModelAndView createSingleView(String rightViewName) {
		ModelAndView mv = new ModelAndView(rightViewName);
		return mv;
	}
	// 分页功能
	/*
	 * protected <T,S> Map<String,Object>getPage(HttpServletRequest request,S
	 * service){ Map<String, Object> map = new HashMap<String, Object>(); int
	 * start = Integer.parseInt(request.getParameter("start")); int length =
	 * Integer.parseInt(request.getParameter("length")); List<T> accounts =
	 * service.getPage(start, length, null, null, null); Long count =
	 * S.getRepository().count(); map.put("data", accounts); map.put("draw",
	 * request.getParameter("draw")); map.put("recordsFiltered", count);
	 * map.put("recordsTotal", count); }
	 */

	/**
	 * 得到当前登录用户账号
	 * 
	 * @return
	 */
	// @Cacheable("account")
	protected Account getMyAccount() {
		Account a = null;
		String crmout = request.getHeader("crm-out");
		String outkey = "D9A61EBF-4CC6-4E49-9561-58A6E34F34DF";
		String userid = request.getHeader("userid");
		if (StringUtils.hasText(crmout) && crmout.equals(outkey) && StringUtils.hasText(userid)) {

			// CRM外部调用
			a = new Account();
			User user = new User();
			user.setId(userid);
			a.setUser(user);

		} else if (this.isMobile()) {
			String accountId = request.getHeader("accountid");
			if (StringUtils.hasText(accountId)) {
				a = accountService.getRepository().getOne(accountId);
			}
		} else
			a = (Account) request.getSession().getAttribute("account");

		return a;
	}

	/**
	 * 在当前SESSION中存储变量
	 * 
	 * @param variableName
	 * @param value
	 */
	protected <T> void addSession(String variableName, T value) {
		request.getSession().setAttribute(variableName, value);
	}

	/**
	 * 从当前SESSION中读取变量
	 * 
	 * @param variableName
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected <T> T getSession(String variableName) {
		T r = null;
		r = (T) request.getSession().getAttribute(variableName);
		return r;
	}

	protected Module getPageModule(String moduleName) {
		Module m = null;
		m = moduleService.getRepository().findByName(moduleName);
		if (null == m)
			return null;
		if (null == this.getMyAccount())
			return null;
		String roleId = this.getMyAccount().getRole().getId();
		M2R m2r = this.m2rService.getRepository().findByRoleIdAndModuleId(roleId, m.getId());
		if (m2r != null) {
			m.setCanAdd(m2r.isAddOnly());
			m.setCanDelete(m2r.isDeleteOnly());
			m.setCanModify(m2r.isModifyOnly());
			m.setCanRead(m2r.isReadOnly());
			m.setCanOther1(m2r.isOther1Only());
			m.setCanOther2(m2r.isOther2Only());
			m.setDataFilter(m2r.getDataFilter());
		}
		return m;
	}

	protected Boolean isMobile() {
		String device = request.getHeader("device");
		return StringUtils.hasText(device) ? device.equals("mobile") : false;
	}

	@RequestMapping(value = PATH)
	public String error() {
		return "出错了";
	}

	@Override
	public String getErrorPath() {
		return PATH;
	}
	protected DateQueryItem getDateQueryItem(String propName){
		String start=request.getParameter(propName+"StartDate");
		String end=request.getParameter(propName+"EndDate");
		DateQueryItem d=null;
		if(StringUtils.hasText(start)){			
			d=new DateQueryItem();
			d.setStartDate(DataConverter.formatDateYMD(start));
		}
		if(StringUtils.hasText(end)){			
			if(d==null) d=new DateQueryItem();
			d.setEndDate(DataConverter.formatDateYMD(end));
		}
		if(null!=d){
			d.setPropertyName(propName);				
		}
		return d;
	}
	protected NumQueryItem getNumQueryItem(String propName){
		String start=request.getParameter(propName+"StartNum");
		String end=request.getParameter(propName+"EndNum");
		NumQueryItem n= new NumQueryItem();
		if(StringUtils.hasText(start)){			
			n.setStartNum(Float.parseFloat(start));
		}else{
			n.setStartNum(0);
		}
		if(StringUtils.hasText(end)){			
			n.setEndNum(Float.parseFloat(end));
		}else{
			n.setEndNum(0);
		}
		if(null!=n){
			n.setPropertyName(propName);				
		}
		return n;
	}
	
	protected IntegerQueryItem getIntegerQueryItem(String propName){
		String start=request.getParameter(propName+"StartNum");
		String end=request.getParameter(propName+"EndNum");
		IntegerQueryItem n=null;
		if(StringUtils.hasText(start)){			
			n=new IntegerQueryItem();
			n.setStartNum(Integer.parseInt(start));
		}else{
			n=new IntegerQueryItem();
			n.setStartNum(0);
		}
		if(StringUtils.hasText(end)){			
			n.setEndNum(Integer.parseInt(end));
		}else{
			n.setEndNum(0);
		}
		if(null!=n){
			n.setPropertyName(propName);				
		}
		return n;
	}
	
	protected DoubleQueryItem getDoubleQueryItem(String propName){
		String start=request.getParameter(propName+"StartNum");
		String end=request.getParameter(propName+"EndNum");
		DoubleQueryItem n=null;
		if(StringUtils.hasText(start)){			
			n=new DoubleQueryItem();
			n.setStartNum(Double.parseDouble(start));
		}else{
			n=new DoubleQueryItem();
			n.setStartNum(0.0);
		}
		if(StringUtils.hasText(end)){			
			n.setEndNum(Double.parseDouble(end));
		}else{
			n.setEndNum(0.0);
		}
		if(null!=n){
			n.setPropertyName(propName);				
		}
		return n;
	}
	
	protected Boolean canReadCustomer(){
		Boolean r=false;
		List<Module> modules=this.getMyAccount().getRole().getModules();
		for(Module m:modules){
			if(m.getTarget().contains("/customer")){
				r=true;
			}
		}
		return r;
	}
	
	
	
	
}
